package com.xlisteven.controller;

import com.xlisteven.entity.Fruits;
import com.xlisteven.entity.Product;
import com.xlisteven.entity.User;
import com.xlisteven.feign.FruitsClient;
import com.xlisteven.service.IUserService;
import com.xlisteven.feign.ProductClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Resource
    private IUserService userService;
    @Autowired
    private ProductClient productClient;
    @Autowired
    private FruitsClient fruitsClient;

    @GetMapping("getOne/{id}")
    public User getOneUser(@PathVariable(name = "id") Long id) {
        //本地调用一次
        User user = userService.getOne(id);
        log.info("查询到封装前用户信息:{}",user);
        //远程调用商品接口
        if (user == null){
            log.error("未查询到用户信息");
            return null;
        }
        List<Product> productList = productClient.getProductList(user.getId());
        user.setProductList(productList);
        log.info("查询到封装后商品用户信息:{}",user);

        List<Fruits> fruitsList = fruitsClient.getFruitsList(user.getId());
        user.setFruitsList(fruitsList);
        log.info("查询到封装后水果用户信息:{}",user);
        return user;
    }

    @PostMapping("insertData")
    public String insertData(@RequestBody User user){
        userService.insertData(user);
        return "插入成功";
    }
}
